var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var log4js = require('log4js');
var log = log4js.getLogger('app');
var constant = require('utils/constant.js');
var mailUtil = require('utils/mail.js');

var index = require('./routes/home.js');
var users = require('./routes/users.js');
var wechat = require('./routes/wechat.js');
// var busApi = require('./routes/busApi.js');
var statistic = require('./routes/statistic.js');

var list = require('./routes/list.js');
(function initLogger() {
	log4js.configure({
		appenders : [
			{
				type : 'console',
			},
			{
				type : "dateFile",
   				filename : "logs/server.log",
   				pattern : "-yyyy-MM-dd",
    			alwaysIncludePattern : false,
			},
		],
		replaceConsole : true,
	});
})();

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');


app.use(log4js.connectLogger(log4js.getLogger("http"), { level: 'auto' }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(statistic('page'));



// app.all('*', function(req, res, next) {
//     res.header("Access-Control-Allow-Origin", "*");
//     res.header("Access-Control-Allow-Headers", "X-Requested-With");
//     res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
//     res.header("X-Powered-By",' 3.2.1')
//     res.header("Content-Type", "application/json;charset=utf-8");
//     next();
// });//设置跨域允许，测试需要，上线的时候需要去掉

app.use('/', index);
app.use('/users', users);
app.use('/wechat',wechat);
// app.use('/busApi',busApi);
app.use('/yyg',list);


// catch 404 and forward to error handler
app.use(function TQW404Handle(req, res, next) {
  var err = new Error(req.path + ' Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {

  // set locals, only providing error in development
  res.locals.message = err.message;
  // res.locals.error = req.app.get('env') === 'development' ? err : {};
  res.locals.error = constant.isDebugVersion ? err : {};

  if(err.status != 404) {
    let msg = err.stack;
    // mailUtil.sendMail(msg, constant.programmerMails);
  }

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

// var https= require('https');
// // var fs = require('fs');
// // // process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// var options = {
//   // pfx:fs.readFileSync('./server.pfx'),
//   // passphrase:'scau_hjd',
//   // // rejectUnauthorized : false,
//   // // // secureOptions : constants.SSL_OP_NO_TLSv1_2,
//   // // strictSSL : false,
//   // // // rejectUnauthorized: false,
//   // //     requestCert: true,
//   // //     agent: false
// };
app.listen(8081);
// https.createServer(options,app).listen(8081);
// app.listen(443);
module.exports = app;
